# Load libraries ----
library(tidyverse)
library(ggthemes)
library(multcomp)
library(gtsummary)
library(effects)
library(ggeffects)
library(gridExtra)
library(cowplot)
library(grid)
library(haven)
library(srvyr)
library(Hmisc)
library(rio)
library(modelsummary)
library(jtools)
library(xtable)
library(ggpubr)
library(rstatix)
library(interactions)
library(stevemisc)
library(lmtest)
library(sandwich)
library(estimatr)
library(dplyr)
library(kableExtra)
library(flextable)
library(gt)

options(scipen = 999) # Turn off scientific notation

# Define themes ----
mytheme <- function(...){
  theme_clean() +   
    theme(plot.title.position="plot",
          text=element_text(family="serif", color="gray15"), 
          axis.title=element_text(size=12, color="gray15"), 
          axis.text=element_text(size=12, color="gray15"),
          plot.title = element_text(size=12, color = "gray15"), 
          plot.caption=element_text(size = 12),  
          legend.position = "bottom",
          plot.background = element_rect(color="white"), 
          axis.title.y = element_text(margin = margin(t=0,r=10,b=0,l=0)), 
          axis.ticks = element_line(color="gray15"), 
          axis.line.x = element_line(color="gray15"), 
          axis.line.y = element_line(color="gray15"),
          panel.grid.major.y = element_line(color = "grey90", linetype = "solid"),
          strip.text.x = element_text(size = 18),
          legend.text = element_text(size = 12),
          legend.title = element_text(size = 12),
          #legend.title = element_blank(),
          legend.margin=margin(2,2,2,2),
          legend.box.margin=margin(-10,-10,0,-10),
          #axis.title.x = element_blank()
    )
  }

mytheme2 <- function(...){
  theme_clean() +   
    theme(plot.title.position="plot",
          text=element_text(family="serif", color="gray15"), 
          axis.title=element_text(size=14, color="gray15"), 
          axis.text=element_text(size=12, color="gray15"),
          plot.title = element_text(size=12, color = "gray15"), 
          plot.caption=element_text(size = 12),  
          legend.position = "bottom",
          plot.background = element_rect(color="white"), 
          legend.background = element_rect(color=NA), 
          axis.ticks = element_line(color="gray15"), 
          axis.line.x = element_line(color="gray15"), 
          axis.line.y = element_line(color="gray15"),
          panel.grid.major.y = element_line(color = "grey90", linetype = "solid"),
          strip.text.x = element_text(size = 18),
          legend.text = element_text(size = 12),
          axis.title.y = element_blank())
  }

mytheme3 <- function(...){
  theme_clean() +   
    theme(plot.title.position="panel",
          text=element_text(family="serif", color="gray15"), 
          axis.title=element_text(size=12, color="gray15"), 
          axis.text.y=element_text(size=12, color="gray15"),
          axis.text.x = element_text(size=12, color="gray15"),
          plot.title = element_text(size=12, color = "gray15"), 
          plot.caption=element_text(size = 10),  
          plot.background = element_rect(color="white"), 
          axis.title.x = element_text(margin = margin(t=0,r=10,b=0,l=0)), 
          axis.ticks = element_line(color="gray15"), 
          axis.line.x = element_line(color="gray15"), 
          axis.line.y = element_line(color="gray15"),
          panel.grid.major.x = element_line(color = "grey90", linetype = "solid"),
          panel.grid.major.y = element_line(color = "white", linetype = "solid"),
          #legend.position = "bottom",
          legend.text = element_text(size = 12),
          legend.title= element_blank(),
          legend.background = element_rect(color=NA),
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-10,-10,0,-10),
          strip.text.x = element_text(size = 10),
          axis.title.y = element_blank())
  }


# Load in data ----
dso = import("Exp.2.data.csv")

ds = dso %>% 
  filter(consent == "I agree to participate", 
         attention =="Moderately interested",
         Finished == "TRUE",
         rid != '',
         debrief != "No, withdraw my responses from the study"
  ) %>% 
  mutate(project_assignment = case_when(FL_338_DO=="Exp_PP.Top_Amount.High" ~ "top.high",
                                        FL_338_DO=="Exp_PP.Top_Amount.Low" ~ "top.low",
                                        FL_338_DO=="Exp_PP.Six_Amount.High" ~ "six.high",
                                        FL_338_DO=="Exp_PP.Six_Amount.Low" ~ "six.low"),
         project_assignment = factor(project_assignment,
                                     levels=c('six.low','six.high','top.low','top.high')),
         priority_treatment = case_when(FL_338_DO=="Exp_PP.Top_Amount.High" ~ "Top",
                                        FL_338_DO=="Exp_PP.Top_Amount.Low" ~ "Top",
                                        FL_338_DO=="Exp_PP.Six_Amount.High" ~ "Six",
                                        FL_338_DO=="Exp_PP.Six_Amount.Low" ~ "Six"),
         priority_treatment = factor(priority_treatment,
                                     levels=c('Six','Top')),
         influence_treatment = case_when(FL_338_DO=="Exp_PP.Top_Amount.High" ~ "High",
                                         FL_338_DO=="Exp_PP.Top_Amount.Low" ~ "Low",
                                         FL_338_DO=="Exp_PP.Six_Amount.High" ~ "High",
                                         FL_338_DO=="Exp_PP.Six_Amount.Low" ~ "Low"),
         influence_treatment = factor(influence_treatment,
                                      levels=c('Low','High')),
         education_rank = issue_ranking_6,
         employment_rank = issue_ranking_7,
         defense_rank = issue_ranking_10,
         healthcare_rank = issue_ranking_9,
         public_safety_rank = issue_ranking_4,
         public_lands_rank = issue_ranking_3,
         environment_rank = issue_ranking_5,
         economy_rank = issue_ranking_11,
         transportation_rank = issue_ranking_2,
         education_therm = case_when(education_rank==1 ~ spending_therm_1,
                                     education_rank==2 ~ spending_therm_2,
                                     education_rank==3 ~ spending_therm_3,
                                     education_rank==4 ~ spending_therm_4,
                                     education_rank==5 ~ spending_therm_5,
                                     education_rank==6 ~ spending_therm_6,
                                     education_rank==7 ~ spending_therm_7,
                                     education_rank==8 ~ spending_therm_8,
                                     education_rank==9 ~ spending_therm_9),
         employment_therm = case_when(employment_rank==1 ~ spending_therm_1,
                                      employment_rank==2 ~ spending_therm_2,
                                      employment_rank==3 ~ spending_therm_3,
                                      employment_rank==4 ~ spending_therm_4,
                                      employment_rank==5 ~ spending_therm_5,
                                      employment_rank==6 ~ spending_therm_6,
                                      employment_rank==7 ~ spending_therm_7,
                                      employment_rank==8 ~ spending_therm_8,
                                      employment_rank==9 ~ spending_therm_9),
         defense_therm = case_when(defense_rank==1 ~ spending_therm_1,
                                   defense_rank==2 ~ spending_therm_2,
                                   defense_rank==3 ~ spending_therm_3,
                                   defense_rank==4 ~ spending_therm_4,
                                   defense_rank==5 ~ spending_therm_5,
                                   defense_rank==6 ~ spending_therm_6,
                                   defense_rank==7 ~ spending_therm_7,
                                   defense_rank==8 ~ spending_therm_8,
                                   defense_rank==9 ~ spending_therm_9),
         healthcare_therm = case_when(healthcare_rank==1 ~ spending_therm_1,
                                      healthcare_rank==2 ~ spending_therm_2,
                                      healthcare_rank==3 ~ spending_therm_3,
                                      healthcare_rank==4 ~ spending_therm_4,
                                      healthcare_rank==5 ~ spending_therm_5,
                                      healthcare_rank==6 ~ spending_therm_6,
                                      healthcare_rank==7 ~ spending_therm_7,
                                      healthcare_rank==8 ~ spending_therm_8,
                                      healthcare_rank==9 ~ spending_therm_9),
         public_safety_therm = case_when(public_safety_rank==1 ~ spending_therm_1,
                                         public_safety_rank==2 ~ spending_therm_2,
                                         public_safety_rank==3 ~ spending_therm_3,
                                         public_safety_rank==4 ~ spending_therm_4,
                                         public_safety_rank==5 ~ spending_therm_5,
                                         public_safety_rank==6 ~ spending_therm_6,
                                         public_safety_rank==7 ~ spending_therm_7,
                                         public_safety_rank==8 ~ spending_therm_8,
                                         public_safety_rank==9 ~ spending_therm_9),
         public_lands_therm = case_when(public_lands_rank==1 ~ spending_therm_1,
                                        public_lands_rank==2 ~ spending_therm_2,
                                        public_lands_rank==3 ~ spending_therm_3,
                                        public_lands_rank==4 ~ spending_therm_4,
                                        public_lands_rank==5 ~ spending_therm_5,
                                        public_lands_rank==6 ~ spending_therm_6,
                                        public_lands_rank==7 ~ spending_therm_7,
                                        public_lands_rank==8 ~ spending_therm_8,
                                        public_lands_rank==9 ~ spending_therm_9),
         environment_therm = case_when(environment_rank==1 ~ spending_therm_1,
                                       environment_rank==2 ~ spending_therm_2,
                                       environment_rank==3 ~ spending_therm_3,
                                       environment_rank==4 ~ spending_therm_4,
                                       environment_rank==5 ~ spending_therm_5,
                                       environment_rank==6 ~ spending_therm_6,
                                       environment_rank==7 ~ spending_therm_7,
                                       environment_rank==8 ~ spending_therm_8,
                                       environment_rank==9 ~ spending_therm_9),
         economy_therm = case_when(economy_rank==1 ~ spending_therm_1,
                                   economy_rank==2 ~ spending_therm_2,
                                   economy_rank==3 ~ spending_therm_3,
                                   economy_rank==4 ~ spending_therm_4,
                                   economy_rank==5 ~ spending_therm_5,
                                   economy_rank==6 ~ spending_therm_6,
                                   economy_rank==7 ~ spending_therm_7,
                                   economy_rank==8 ~ spending_therm_8,
                                   economy_rank==9 ~ spending_therm_9),
         transportation_therm = case_when(transportation_rank==1 ~ spending_therm_1,
                                          transportation_rank==2 ~ spending_therm_2,
                                          transportation_rank==3 ~ spending_therm_3,
                                          transportation_rank==4 ~ spending_therm_4,
                                          transportation_rank==5 ~ spending_therm_5,
                                          transportation_rank==6 ~ spending_therm_6,
                                          transportation_rank==7 ~ spending_therm_7,
                                          transportation_rank==8 ~ spending_therm_8,
                                          transportation_rank==9 ~ spending_therm_9),
         education_allocate = case_when(education_rank==1 ~ Allocation_2,
                                        education_rank==2 ~ Allocation_3,
                                        education_rank==3 ~ Allocation_4,
                                        education_rank==4 ~ Allocation_5,
                                        education_rank==5 ~ Allocation_6,
                                        education_rank==6 ~ Allocation_7,
                                        education_rank==7 ~ Allocation_8,
                                        education_rank==8 ~ Allocation_9,
                                        education_rank==9 ~ Allocation_10),
         employment_allocate = case_when(employment_rank==1 ~ Allocation_2,
                                         employment_rank==2 ~ Allocation_3,
                                         employment_rank==3 ~ Allocation_4,
                                         employment_rank==4 ~ Allocation_5,
                                         employment_rank==5 ~ Allocation_6,
                                         employment_rank==6 ~ Allocation_7,
                                         employment_rank==7 ~ Allocation_8,
                                         employment_rank==8 ~ Allocation_9,
                                         employment_rank==9 ~ Allocation_10),
         defense_allocate = case_when(defense_rank==1 ~ Allocation_2,
                                      defense_rank==2 ~ Allocation_3,
                                      defense_rank==3 ~ Allocation_4,
                                      defense_rank==4 ~ Allocation_5,
                                      defense_rank==5 ~ Allocation_6,
                                      defense_rank==6 ~ Allocation_7,
                                      defense_rank==7 ~ Allocation_8,
                                      defense_rank==8 ~ Allocation_9,
                                      defense_rank==9 ~ Allocation_10),
         healthcare_allocate = case_when(healthcare_rank==1 ~ Allocation_2,
                                         healthcare_rank==2 ~ Allocation_3,
                                         healthcare_rank==3 ~ Allocation_4,
                                         healthcare_rank==4 ~ Allocation_5,
                                         healthcare_rank==5 ~ Allocation_6,
                                         healthcare_rank==6 ~ Allocation_7,
                                         healthcare_rank==7 ~ Allocation_8,
                                         healthcare_rank==8 ~ Allocation_9,
                                         healthcare_rank==9 ~ Allocation_10),
         public_safety_allocate = case_when(public_safety_rank==1 ~ Allocation_2,
                                            public_safety_rank==2 ~ Allocation_3,
                                            public_safety_rank==3 ~ Allocation_4,
                                            public_safety_rank==4 ~ Allocation_5,
                                            public_safety_rank==5 ~ Allocation_6,
                                            public_safety_rank==6 ~ Allocation_7,
                                            public_safety_rank==7 ~ Allocation_8,
                                            public_safety_rank==8 ~ Allocation_9,
                                            public_safety_rank==9 ~ Allocation_10),
         public_lands_allocate = case_when(public_lands_rank==1 ~ Allocation_2,
                                           public_lands_rank==2 ~ Allocation_3,
                                           public_lands_rank==3 ~ Allocation_4,
                                           public_lands_rank==4 ~ Allocation_5,
                                           public_lands_rank==5 ~ Allocation_6,
                                           public_lands_rank==6 ~ Allocation_7,
                                           public_lands_rank==7 ~ Allocation_8,
                                           public_lands_rank==8 ~ Allocation_9,
                                           public_lands_rank==9 ~ Allocation_10),
         environment_allocate = case_when(environment_rank==1 ~ Allocation_2,
                                          environment_rank==2 ~ Allocation_3,
                                          environment_rank==3 ~ Allocation_4,
                                          environment_rank==4 ~ Allocation_5,
                                          environment_rank==5 ~ Allocation_6,
                                          environment_rank==6 ~ Allocation_7,
                                          environment_rank==7 ~ Allocation_8,
                                          environment_rank==8 ~ Allocation_9,
                                          environment_rank==9 ~ Allocation_10),
         economy_allocate = case_when(economy_rank==1 ~ Allocation_2,
                                      economy_rank==2 ~ Allocation_3,
                                      economy_rank==3 ~ Allocation_4,
                                      economy_rank==4 ~ Allocation_5,
                                      economy_rank==5 ~ Allocation_6,
                                      economy_rank==6 ~ Allocation_7,
                                      economy_rank==7 ~ Allocation_8,
                                      economy_rank==8 ~ Allocation_9,
                                      economy_rank==9 ~ Allocation_10),
         transportation_allocate = case_when(transportation_rank==1 ~ Allocation_2,
                                             transportation_rank==2 ~ Allocation_3,
                                             transportation_rank==3 ~ Allocation_4,
                                             transportation_rank==4 ~ Allocation_5,
                                             transportation_rank==5 ~ Allocation_6,
                                             transportation_rank==6 ~ Allocation_7,
                                             transportation_rank==7 ~ Allocation_8,
                                             transportation_rank==8 ~ Allocation_9,
                                             transportation_rank==9 ~ Allocation_10),
         post_feel = case_when(post_feel_top.high_34!="" ~ post_feel_top.high_34,
                               post_feel_top.low_34!="" ~ post_feel_top.low_34,
                               post_feel_six.high_34!="" ~ post_feel_six.high_34,
                               post_feel_six.low_34!="" ~ post_feel_six.low_34),
         post_effect = case_when(post_effect_top.high_34!="" ~ post_effect_top.high_34,
                                 post_effect_top.low_34!="" ~ post_effect_top.low_34,
                                 post_effect_six.high_34!="" ~ post_effect_six.high_34,
                                 post_effect_six.low_34!="" ~ post_effect_six.low_34),
         post_rep = case_when(post_rep_top.high_34!="" ~ post_rep_top.high_34,
                              post_rep_top.low_34!="" ~ post_rep_top.low_34,
                              post_rep_six.high_34!="" ~ post_rep_six.high_34,
                              post_rep_six.low_34!="" ~ post_rep_six.low_34),
         post_resp = case_when(post_resp_top.high_34!="" ~ post_resp_top.high_34,
                               post_resp_top.low_34!="" ~ post_resp_top.low_34,
                               post_resp_six.high_34!="" ~ post_resp_six.high_34,
                               post_resp_six.low_34!="" ~ post_resp_six.low_34),
         pre_feeling = feeling_grid_2,
         pre_effective = effective_grid_2,
         pre_represent = representation_grid_2,
         pre_responsible = responsible_grid_2,
         pre_feeling_other = feeling_grid_6,
         pre_effective_other = effective_grid_6,
         pre_represent_other = representation_grid_6,
         pre_responsible_other = responsible_grid_6,
         feeling_change =  post_feel - pre_feeling,
         effective_change =  post_effect - pre_effective,
         represent_change =  post_rep - pre_represent,
         responsible_change =  post_resp - pre_responsible,
         partyr = case_when(party=="Not very strong Democrat" | party=="Strong Democrat" ~ "dem",
                            party=="Not very strong Republican" | party=="Strong Republican" ~ "rep",
                            party=="Independent" & independent=="More similar to Democrats" ~ "ind",
                            party=="Independent" & independent=="More similar to Republicans" ~ "ind"), 
         partytwo = case_when(party=="Not very strong Democrat" | party=="Strong Democrat" ~ "dem",
                              party=="Not very strong Republican" | party=="Strong Republican" ~ "rep",
                              party=="Independent" & independent=="More similar to Democrats" ~ "dem",
                              party=="Independent" & independent=="More similar to Republicans" ~ "rep"),
         pid7 = case_when(party=="Strong Democrat" ~ "strong_dem",
                          party=="Not very strong Democrat" ~ "not_strong_dem",
                          party=="Independent" & independent=="More similar to Democrats" ~ "lean_dem",
                          party=="Independent" & independent=="Neither" ~ "ind",
                          party=="Independent" & independent=="More similar to Republicans" ~ "lean_rep",
                          party=="Not very strong Republican" ~ "not_strong_rep",
                          party=="Strong Republican" ~ "strong_rep"),
         sen_p = substr(Par1, 2, 2),
         sen_party = case_when(sen_p=="D" | sen_p== "I" ~ "dem",
                               sen_p=="R" ~ "rep"),
         party_match = case_when(partyr=="dem" & sen_party =="dem" ~ 1,
                                 partyr=="rep" & sen_party =="rep" ~ 1,
                                 partyr=="dem" & sen_party =="rep" ~ -1,
                                 partyr=="rep" & sen_party =="dem" ~ -1,
                                 partyr=="ind"~ 0),
         party_match_f = as.factor(party_match),
         income = case_when(income=="Under $10,000" ~ 1, 
                            income=="$10,000-$19,999"~2,
                            income=="$20,000-$29,999" ~ 3,
                            income=="$40,000-$49,999"~4,
                            income=="$50,000-$59,999"~5,
                            income=="$30,000-$39,999" ~ 6,
                            income=="$60,000-$74,999"~7,
                            income=="$75,000-$89,999" ~ 8,
                            income=="$90,000-$119,999"~9,
                            income=="$120,000-$159,999"~10,
                            income=="$160,000-$200,000"~11,
                            income=="Over $200,000"~12),
         resp_gender = case_when(gender== 1  ~ "man",
                                 gender== 2  ~ "woman"),
         age = as.numeric(age), 
         education = case_when(education==1|education==2|education==3|education==4|education==5 ~ "Less_than_Bachelors", 
                               education==6|education==7|education==8 ~ "Bachelors_or_higher"),
         ideo = case_when(ideology=="Very Conservative" ~ 1, 
                          ideology=="Conservative" ~ 2, 
                          ideology=="Slightly Conservative" ~ 3, 
                          ideology=="Moderate" ~ 4, 
                          ideology=="Slightly Liberal" ~ 5, 
                          ideology=="Liberal" ~ 6, 
                          ideology=="Very Liberal" ~ 7),
         black = ifelse(grepl("Black or African American", race), 1, 0), 
         hisplat = ifelse(grepl("Hispanic or Latino", race), 1, 0),
         asam = ifelse(grepl("Asian or Pacific Islander", race), 1, 0),
         natam = ifelse(grepl("American Indian or Alaska Native", race), 1, 0),
         white = ifelse(grepl("White", race), 1, 0),
         multiracial = ifelse(grepl("Multi-racial", race), 1, 0),
         rother = ifelse(grepl("Other", race), 1, 0),
         race = case_when(race=="White" ~ "White",
                          race=="Asian or Pacific Islander" ~ "Asian American",
                          race=="Black or African American" ~ "Black",
                          race=="Hispanic or Latino" ~ "Latino",
                          grepl("Multi-racial", race) ~ "Multi-racial",
                          race=="American Indian or Alaska Native" ~ "Other",
                          race=="Other" ~ "Other",
                          grepl(",", race) ~ "Other"),
         hispanic = case_when(hispanic == 1 ~ 0,
                              hispanic == 2 ~ 1,
                              hispanic == 3 ~ 1,
                              hispanic == 4 ~ 1,
                              hispanic == 5 ~ 1,
                              hispanic == 6 ~ 1,
                              hispanic == 7 ~ 1,
                              hispanic == 8 ~ 1,
                              hispanic == 9 ~ 1,
                              hispanic == 10 ~ 1,
                              hispanic == 11 ~ 1,
                              hispanic == 12 ~ 0,
                              hispanic == 13 ~ 1,
                              hispanic == 14 ~ 1,
                              hispanic == 15 ~ 0,
                              hispanic == 16 ~ 1),
         parent = factor(parent, levels=c("No", "Yes")),
         region = case_when(region == 1 ~ "Northeast",
                            region == 2 ~ "Midwest",
                            region == 3 ~ "South",
                            region == 4 ~ "West")) 



# Appendix B ----

# Table B1
ds %>% 
  select(partyr,
         resp_gender,
         age,
         race, 
         hispanic,
         education, 
         income,
         parent) %>% 
  tbl_summary(missing = "no",
              label = c(partyr ~ "Party",
                        resp_gender ~ "Gender", 
                        age ~ "Age",
                        race ~ "Race",
                        hispanic ~ "Hispanic",
                        education ~ "Education",
                        income ~ "Income",
                        parent ~ "Parent")) %>% 
  italicize_levels() %>%
  modify_header(label ~ "**Variable**") -> Table_b1


## Project assignment balance
ds %>% 
  select(partyr,
         resp_gender,
         age,
         race, 
         hispanic,
         education, 
         income,
         parent,
         priority_treatment
  ) %>% 
  tbl_summary(by=priority_treatment, 
              missing = "no", 
              label = c(partyr ~ "Party",
                        resp_gender ~ "Gender", 
                        age ~ "Age",
                        race ~ "Race",
                        hispanic ~ "Hispanic",
                        education ~ "Education",
                        income ~ "Income",
                        parent ~ "Parent")) %>% 
  add_p()%>%
  italicize_levels() %>%
  modify_header(label ~ "**Variable**") -> table_b2

## Senate influence balance
ds %>% 
  select(partyr,
         resp_gender,
         age,
         race, 
         hispanic,
         education, 
         income,
         parent,
         influence_treatment
  ) %>% 
  tbl_summary(by=influence_treatment, 
              missing = "no", 
              label = c(partyr ~ "Party",
                        resp_gender ~ "Gender", 
                        age ~ "Age",
                        race ~ "Race",
                        hispanic ~ "Hispanic",
                        education ~ "Education",
                        income ~ "Income",
                        parent ~ "Parent")) %>% 
  add_p()%>%
  italicize_levels() %>%
  modify_header(label ~ "**Variable**") -> table_b3

# Appendix D ----

# Figure D1
ds %>% 
  select(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank) %>% 
  pivot_longer(
    cols = c(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank),
    cols_vary = "fastest",
    names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = 10 - mean(value),  # reverse-coded
            se = sd(10 - value)/sqrt(nrow),
            ci = 1.96 * se) %>% 
  mutate(type = factor(type, level=c('public_lands_rank','defense_rank','environment_rank',
                                     'transportation_rank','economy_rank','public_safety_rank',
                                     'employment_rank','education_rank','healthcare_rank'))) %>%
  ggplot(aes(x = type, y = mean)) +
  geom_pointrange(aes(ymin = mean - ci, ymax = mean + ci), size = 0.25, linewidth = 0.5) +
  labs(y = "Mean Ranking Score (Higher = More Important)") +  
  scale_x_discrete(labels = c(
    "education_rank" = "Education",
    "employment_rank" = "Employement",
    "defense_rank" = "Defense",
    "healthcare_rank" = "Healthcare",
    "public_safety_rank" = "Public Safety",
    "public_lands_rank" = "Public Lands",
    "environment_rank" = "Environment",
    "economy_rank" = "Local Economy",
    "transportation_rank" = "Transportation"
  )) +
  geom_vline(xintercept = c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5), 
             color = "lightgrey", linetype = "dashed") +
  coord_flip() +
  mytheme3() -> figure_d1

# Figure d2
ds %>% 
  select(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank, partyr) %>% 
  pivot_longer(
    cols = c(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank),
    cols_vary = "fastest",
    names_to = "type") %>% 
  drop_na(type, value, partyr) %>%
  group_by(type, partyr) %>% 
  summarise(nrow = length(value), 
            mean = 10 - mean(value),  # reverse-coded
            se = sd(10 - value) / sqrt(nrow),
            ci = 1.96 * se) %>%
  mutate(type = factor(type, level=c('public_lands_rank','defense_rank','environment_rank',
                                     'transportation_rank','economy_rank','public_safety_rank',
                                     'employment_rank','education_rank','healthcare_rank'))) %>%  
  ggplot(aes(x = type, y = mean, shape = partyr, color = partyr)) +
  geom_pointrange(aes(ymin = mean - ci, ymax = mean + ci), 
                  position = position_dodge(width = 0.5), 
                  size = 0.25, linewidth = 0.5) +
  labs(shape = "", y = "Mean Ranking Score (Higher = More Important)") +
  scale_x_discrete(labels = c(
    "education_rank" = "Education",
    "employment_rank" = "Employement",
    "defense_rank" = "Defense",
    "healthcare_rank" = "Healthcare",
    "public_safety_rank" = "Public Safety",
    "public_lands_rank" = "Public Lands",
    "environment_rank" = "Environment",
    "economy_rank" = "Local Economy",
    "transportation_rank" = "Transportation"
  )) +
  scale_shape_manual(values = c(15, 16, 17), labels = c("Dem", "Ind", "Rep"), name = "Resp. Party") +
  scale_color_manual(values = c("mediumblue", "green4", "red3"), labels = c("Dem", "Ind", "Rep"), name = "Resp. Party") +
  geom_vline(xintercept = c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5), 
             color = "lightgrey", linetype = "dashed") +
  coord_flip() +
  guides(color = guide_legend(reverse = TRUE), shape = guide_legend(reverse = TRUE)) +
  mytheme3() +
  theme(legend.position = c(0.905, 0.135)) -> figure_d2

# Figure D3
ds %>% 
  select(education_therm, employment_therm, defense_therm, healthcare_therm, public_safety_therm, public_lands_therm, environment_therm, economy_therm, transportation_therm, partyr) %>% 
  pivot_longer(
    cols = c(education_therm, employment_therm, defense_therm, healthcare_therm, public_safety_therm, public_lands_therm, environment_therm, economy_therm, transportation_therm),
    cols_vary = "fastest",
    names_to = "type") %>% 
  drop_na(type, value, partyr) %>%
  group_by(type, partyr) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value)/sqrt(nrow),
            ci = 1.96*se) %>% 
  ggplot(., aes(x=factor(type, level=c('public_lands_therm','defense_therm','environment_therm','transportation_therm','economy_therm','public_safety_therm','employment_therm','education_therm','healthcare_therm')), y = mean, shape = partyr, color = partyr)) + 
  geom_pointrange(aes(ymin = mean-ci, ymax = mean+ci), position = position_dodge(width = .5), size = 0.25, linewidth=0.5) + 
  labs(shape = "",
       y = "Mean Rating (0-100) Score") +  
  scale_x_discrete(labels=c("education_therm" = "Education", "employment_therm" = "Employement", "defense_therm" = "Defense", "healthcare_therm" = "Healthcare", "public_safety_therm" = "Public Safety", "public_lands_therm" = "Public Lands", "environment_therm" = "Environment", "economy_therm" = "Local Economy", "transportation_therm" = "Transportation")) +
  scale_shape_manual(values=c(15, 16, 17), labels = c("Dem","Ind","Rep"), name = "Resp. Party")+
  scale_color_manual(values=c("mediumblue","green4","red3"), labels = c("Dem","Ind","Rep"), name = "Resp. Party")+
  geom_vline(xintercept=c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5), color="lightgrey", linetype = "dashed")+
  coord_flip()+
  guides(color = guide_legend(reverse = TRUE), shape = guide_legend(reverse = TRUE))+
  mytheme3() +
  theme(legend.position = c(0.905, 0.135)) -> figure_d3

# Figure D4
ds %>% 
  select(education_allocate, employment_allocate, defense_allocate, healthcare_allocate, public_safety_allocate, public_lands_allocate, environment_allocate, economy_allocate, transportation_allocate, partyr) %>% 
  pivot_longer(
    cols = c(education_allocate, employment_allocate, defense_allocate, healthcare_allocate, public_safety_allocate, public_lands_allocate, environment_allocate, economy_allocate, transportation_allocate),
    cols_vary = "fastest",
    names_to = "type") %>% 
  drop_na(type, value, partyr) %>%
  group_by(type, partyr) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value)/sqrt(nrow),
            ci = 1.96*se) %>% 
  ggplot(., aes(x=factor(type, level=c('public_lands_allocate', 'defense_allocate','environment_allocate','transportation_allocate','economy_allocate','public_safety_allocate','employment_allocate','education_allocate', 'healthcare_allocate')), y = mean, shape = partyr, color = partyr)) + 
  geom_pointrange(aes(ymin = mean-ci, ymax = mean+ci), position = position_dodge(width = .5), size = 0.25, linewidth=0.5) + 
  labs(shape = "",
       y = "Mean Spending Category Rating (0-100)") +  
  scale_x_discrete(labels=c("education_allocate" = "Education", "employment_allocate" = "Employement", "defense_allocate" = "Defense", "healthcare_allocate" = "Healthcare", "public_safety_allocate" = "Public Safety", "public_lands_allocate" = "Public Lands", "environment_allocate" = "Environment", "economy_allocate" = "Local Economy", "transportation_allocate" = "Transportation")) +
  scale_shape_manual(values=c(15, 16, 17), labels = c("Dem","Ind","Rep"), name = "Resp. Party")+
  scale_color_manual(values=c("mediumblue","green4","red3"), labels = c("Dem","Ind","Rep"), name = "Resp. Party")+
  geom_vline(xintercept=c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5),color="lightgrey", linetype = "dashed")+
  coord_flip()+
  guides(color = guide_legend(reverse = TRUE), shape = guide_legend(reverse = TRUE))+
  mytheme3() +
  theme(legend.position = c(0.905, 0.135)) -> figure_d4

#Table D1
ds %>% 
  select(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank) %>% 
  pivot_longer(
    cols = c(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank),
    cols_vary = "fastest",
    names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = 10 - mean(value), 
            se = sd(10 - value)/sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop") %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption = "Mean and 95% CI for Spending Category Ranking (Higher = More Important)",
      col.names = c("Type", "Mean", "Lower %95 CI", "Upper %95 CI"),
      align = "r") %>%
  kable_minimal(full_width = FALSE, html_font = "Serif")

# Table D2
ds %>% 
  filter(partyr == "dem") %>% 
  select(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank) %>% 
  pivot_longer(
    cols = c(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank),
    cols_vary = "fastest",
    names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = 10 - mean(value), 
            se = sd(10 - value)/sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop"
  ) %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption="Democrats -- Mean and 95% CI for Spending Category Ranking",
      #format="latex",
      col.names = c("Type","Mean","Lower %95 CI","Upper %95 CI"),
      align="r") %>%
  kable_minimal(full_width = F,  html_font = "Seriff")

# Table D3
ds %>% 
  filter(partyr == "rep") %>% 
  select(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank) %>% 
  pivot_longer(
    cols = c(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank),
    cols_vary = "fastest",
    names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = 10 - mean(value), 
            se = sd(10 - value)/sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop"
  ) %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption="Republicans -- Mean and 95% CI for Spending Category Ranking",
     # format="latex",
      col.names = c("Type","Mean","Lower %95 CI","Upper %95 CI"),
      align="r") %>%
  kable_minimal(full_width = F,  html_font = "Seriff")

#Table D4
ds %>% 
  filter(partyr == "ind") %>% 
  select(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank) %>% 
  pivot_longer(
    cols = c(education_rank, employment_rank, defense_rank, healthcare_rank, public_safety_rank, public_lands_rank, environment_rank, economy_rank, transportation_rank),
    cols_vary = "fastest",
    names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = 10 - mean(value), 
            se = sd(10 - value)/sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop"
  ) %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption="Independents -- Mean and 95% CI for Spending Category Ranking",
     # format="latex",
      col.names = c("Type","Mean","Lower %95 CI","Upper %95 CI"),
      align="r") %>%
  kable_minimal(full_width = F,  html_font = "Seriff")

# Table D5
ds %>% 
  select(education_therm, employment_therm, defense_therm, healthcare_therm, public_safety_therm, public_lands_therm, environment_therm, economy_therm, transportation_therm) %>% 
  pivot_longer(cols = everything(), names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value) / sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop") %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption = "All Respondents – Mean and 95% CI for Spending Category Ratings",
      col.names = c("Type", "Mean", "Lower %95 CI", "Upper %95 CI"),
      align = "r") %>%
  kable_minimal(full_width = FALSE, html_font = "Serif")

# Table D6
ds %>% 
  filter(partyr == "dem") %>% 
  select(education_therm, employment_therm, defense_therm, healthcare_therm, public_safety_therm, public_lands_therm, environment_therm, economy_therm, transportation_therm) %>% 
  pivot_longer(cols = everything(), names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value) / sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop") %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption = "Democrats – Mean and 95% CI for Spending Category Ratings",
      col.names = c("Type", "Mean", "Lower %95 CI", "Upper %95 CI"),
      align = "r") %>%
  kable_minimal(full_width = FALSE, html_font = "Serif")

# Table D7
ds %>% 
  filter(partyr == "rep") %>% 
  select(education_therm, employment_therm, defense_therm, healthcare_therm, public_safety_therm, public_lands_therm, environment_therm, economy_therm, transportation_therm) %>% 
  pivot_longer(cols = everything(), names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value) / sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop") %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption = "Republicans – Mean and 95% CI for Spending Category Ratings",
      col.names = c("Type", "Mean", "Lower %95 CI", "Upper %95 CI"),
      align = "r") %>%
  kable_minimal(full_width = FALSE, html_font = "Serif")

# Table D8
ds %>% 
  filter(partyr == "ind") %>% 
  select(education_therm, employment_therm, defense_therm, healthcare_therm, public_safety_therm, public_lands_therm, environment_therm, economy_therm, transportation_therm) %>% 
  pivot_longer(cols = everything(), names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value) / sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop") %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption = "Independents – Mean and 95% CI for Spending Category Ratings",
      col.names = c("Type", "Mean", "Lower %95 CI", "Upper %95 CI"),
      align = "r") %>%
  kable_minimal(full_width = FALSE, html_font = "Serif")

# Table D9
ds %>% 
  select(education_allocate, employment_allocate, defense_allocate, healthcare_allocate, public_safety_allocate, public_lands_allocate, environment_allocate, economy_allocate, transportation_allocate) %>% 
  pivot_longer(cols = everything(), names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value) / sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop") %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption = "All Respondents – Mean and 95% CI for Spending Allocation",
      col.names = c("Type", "Mean", "Lower %95 CI", "Upper %95 CI"),
      align = "r") %>%
  kable_minimal(full_width = FALSE, html_font = "Serif")

# Table D10
ds %>% 
  filter(partyr == "dem") %>% 
  select(education_allocate, employment_allocate, defense_allocate, healthcare_allocate, public_safety_allocate, public_lands_allocate, environment_allocate, economy_allocate, transportation_allocate) %>% 
  pivot_longer(cols = everything(), names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value) / sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop") %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption = "Democrats – Mean and 95% CI for Spending Allocation",
      col.names = c("Type", "Mean", "Lower %95 CI", "Upper %95 CI"),
      align = "r") %>%
  kable_minimal(full_width = FALSE, html_font = "Serif")

# Table D11
ds %>% 
  filter(partyr == "rep") %>% 
  select(education_allocate, employment_allocate, defense_allocate, healthcare_allocate, public_safety_allocate, public_lands_allocate, environment_allocate, economy_allocate, transportation_allocate) %>% 
  pivot_longer(cols = everything(), names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value) / sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop") %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption = "Republicans – Mean and 95% CI for Spending Allocation",
      col.names = c("Type", "Mean", "Lower %95 CI", "Upper %95 CI"),
      align = "r") %>%
  kable_minimal(full_width = FALSE, html_font = "Serif")

# Table D12
ds %>% 
  filter(partyr == "ind") %>% 
  select(education_allocate, employment_allocate, defense_allocate, healthcare_allocate, public_safety_allocate, public_lands_allocate, environment_allocate, economy_allocate, transportation_allocate) %>% 
  pivot_longer(cols = everything(), names_to = "type") %>% 
  drop_na(type, value) %>%
  group_by(type) %>% 
  summarise(nrow = length(value), 
            mean = mean(value), 
            se = sd(value) / sqrt(nrow),
            ci = 1.96 * se,
            ci_low = mean - ci,
            ci_high = mean + ci,
            .groups = "drop") %>%
  select(type, mean, ci_low, ci_high) %>% 
  kbl(caption = "Independents – Mean and 95% CI for Spending Allocation",
      col.names = c("Type", "Mean", "Lower %95 CI", "Upper %95 CI"),
      align = "r") %>%
  kable_minimal(full_width = FALSE, html_font = "Serif")

#Appendix G ----

# Table G1
summary(feel_mod <- lm(feeling_change ~  
                         priority_treatment + influence_treatment 
                       , data = ds))

summary(effect_mod <- lm(effective_change ~  
                           priority_treatment + influence_treatment 
                         , data = ds))

summary(rep_mod <- lm(represent_change ~  
                        priority_treatment + influence_treatment 
                      , data = ds))

summary(resp_mod <- lm(responsible_change ~  
                         priority_treatment + influence_treatment 
                       , data = ds))

models <- list()
models[['Thermometer \n(0-100)']] <- feel_mod
models[['Effectiveness \n(0-10)']] <- effect_mod
models[['Representation \n(0-10)']] <- rep_mod
models[['Responsibility \n(0-10)']] <- resp_mod

coefs <- c("priority_treatmentTop" = "Project Demand",
           "influence_treatmentHigh" = "Senator Influence",
           "(Intercept)" = "Constant")

options("modelsummary_format_numeric_latex" = "plain")

modelsummary(models, 
             stars = c('*' = .1, '**' = .05, '***' = .01),
             coef_map = coefs,
             fmt = 2,
             title = '',
             gof_omit = 'R2 Adj.|Log.Lik.|F|AIC|BIC|RMSE', 
             #output = "latex"
             )

# Table G2
summary(feel_mod_i <- lm(feeling_change ~  
                           priority_treatment * influence_treatment 
                         , data = ds))

summary(effect_mod_i <- lm(effective_change ~  
                             priority_treatment * influence_treatment 
                           , data = ds))

summary(rep_mod_i <- lm(represent_change ~  
                          priority_treatment * influence_treatment 
                        , data = ds))

summary(resp_mod_i <- lm(responsible_change ~  
                           priority_treatment * influence_treatment 
                         , data = ds))
models_i <- list()
models_i[['Thermometer \n(0-100)']] <- feel_mod_i
models_i[['Effectiveness \n(0-10)']] <- effect_mod_i
models_i[['Representation \n(0-10)']] <- rep_mod_i
models_i[['Responsibility \n(0-10)']] <- resp_mod_i

coefs_i <- c("priority_treatmentTop" = "Project Demand",
             "influence_treatmentHigh" = "Senator Influence",
             "priority_treatmentTop:influence_treatmentHigh" = "Project Demand X Senator Influence",
             "(Intercept)" = "Constant")

modelsummary(models_i, 
             stars = c('*' = .1, '**' = .05, '***' = .01),
             coef_map = coefs_i,
             fmt = 2,
             title = '',
             gof_omit = 'R2 Adj.|Log.Lik.|F|AIC|BIC|RMSE', 
             #output = "latex"
             )

# Table G3
summary(feel_mod_i_pp <- lm(feeling_change ~  
                              priority_treatment * partytwo + influence_treatment 
                            , data = ds))

summary(effect_mod_i_pp <- lm(effective_change ~  
                                priority_treatment * partytwo + influence_treatment 
                              , data = ds))

summary(rep_mod_i_pp <- lm(represent_change ~  
                             priority_treatment * partytwo + influence_treatment 
                           , data = ds))

summary(resp_mod_i_pp <- lm(responsible_change ~  
                              priority_treatment * partytwo + influence_treatment 
                            , data = ds))

models_i_pp <- list()
models_i_pp[['Thermometer \n(0-100)']] <- feel_mod_i_pp
models_i_pp[['Effectiveness \n(0-10)']] <- effect_mod_i_pp
models_i_pp[['Representation \n(0-10)']] <- rep_mod_i_pp
models_i_pp[['Responsibility \n(0-10)']] <- resp_mod_i_pp

coefs_party <- c("priority_treatmentTop" = "Project Demand",
                 "influence_treatmentHigh" = "Senator Influence",
                 "partytworep" ="Republican Respondent",
                 "priority_treatmentTop:partytworep" = "Project Demand X Republican",
                 "influence_treatmentHigh:partytworep" = "Senator Influence X Republican",
                 "(Intercept)" = "Constant")

modelsummary(models_i_pp, 
             stars = c('*' = .1, '**' = .05, '***' = .01),
             coef_map = coefs_party,
             fmt = 2,
             title = '',
             gof_omit = 'R2 Adj.|Log.Lik.|F|AIC|BIC|RMSE', 
             #output = "latex"
             )
# Table G4
summary(feel_mod_i_pi <- lm(feeling_change ~  
                              priority_treatment + influence_treatment * partytwo 
                            , data = ds))

summary(effect_mod_i_pi <- lm(effective_change ~  
                                priority_treatment + influence_treatment * partytwo
                              , data = ds))

summary(rep_mod_i_pi <- lm(represent_change ~  
                             priority_treatment + influence_treatment * partytwo
                           , data = ds))

summary(resp_mod_i_pi <- lm(responsible_change ~  
                              priority_treatment + influence_treatment * partytwo
                            , data = ds))
models_i_pi <- list()
models_i_pi[['Thermometer \n(0-100)']] <- feel_mod_i_pi
models_i_pi[['Effectiveness \n(0-10)']] <- effect_mod_i_pi
models_i_pi[['Representation \n(0-10)']] <- rep_mod_i_pi
models_i_pi[['Responsibility \n(0-10)']] <- resp_mod_i_pi

modelsummary(models_i_pi, 
             stars = c('*' = .1, '**' = .05, '***' = .01),
             coef_map = coefs_party,
             fmt = 2,
             title = '',
             gof_omit = 'R2 Adj.|Log.Lik.|F|AIC|BIC|RMSE', 
             #output = "latex"
             )

#Table G5
t.test(ds$feeling_change[ds$project_assignment=='top.low'],ds$feeling_change[ds$project_assignment=='six.high']) -> t.test.therm
t.test(ds$effective_change[ds$project_assignment=='top.low'],ds$effective_change[ds$project_assignment=='six.high']) -> t.test.eff
t.test(ds$represent_change[ds$project_assignment=='top.low'],ds$represent_change[ds$project_assignment=='six.high']) -> t.test.rep
t.test(ds$responsible_change[ds$project_assignment=='top.low'],ds$responsible_change[ds$project_assignment=='six.high']) -> t.test.resp

pvalue_format <- function(x){
  z <- cut(as.numeric(x), breaks = c(-Inf, 0.01, 0.05, 0.1, Inf), labels = c("***", "**", "*", ""))
  as.character(z)
}

tab_one <- map_df(list(t.test.therm, t.test.eff, t.test.rep, t.test.resp), .id = "dv", tidy)  
table_one <- tab_one[c("dv", "estimate", "estimate1", "estimate2", "p.value")]
DV <- c("Feeling Therm. Rating (0-100) Change","Effectiveness Rating (0-10) Change","Representation Rating (0-10) Change","Responsibility Rating (0-10) Change")
table_one$DV <- DV

flextable(table_one,
          col_keys = c("DV", "estimate1", "estimate2", "estimate", "p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>% 
  set_header_labels(COMP = "Comparison", estimate = "Diff. of Means",
                    estimate1 = "High Priority / Low Influence", estimate2 = "Low Priority / High Influence",
                    p.value = "P-value", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> table_g5

#Table G6
therm.anova = aov(feeling_change ~ project_assignment, ds)
TukeyHSD(therm.anova, conf.level=.95) -> anova.therm.mod.results
anova.therm.mod.results.df <- map_df(list(anova.therm.mod.results), .id = "dv", tidy)
anova.therm.results.df <- anova.therm.mod.results.df[c("contrast", "estimate", "adj.p.value")]

Con <- c("Low Priority/High Influence - Low Priority/Low Influence",
         "High Priority/Low Influence - Low Priority/Low Influence",
         "High Priority/High Influence - Low Priority/Low Influence",
         "High Priority/Low Influence - Low Priority/High Influence",
         "High Priority/High Influence - Low Priority/High Influence",
         "High Priority/High Influence - High Priority/Low Influence")

anova.therm.results.df$Con <- Con

flextable(anova.therm.results.df,
          col_keys = c("Con", "estimate", "adj.p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(adj.p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", 
                    Con = "Contrast", estimate = "Diff. of Means",
                    adj.p.value = "Adjusted Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "adj.p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> table_g6

#Table G7
eff.anova = aov(effective_change ~ project_assignment, ds)
TukeyHSD(eff.anova, conf.level=.95) -> anova.eff.mod.results

anova.eff.mod.results.df <- map_df(list(anova.eff.mod.results), .id = "dv", tidy)
anova.eff.results.df <- anova.eff.mod.results.df[c("contrast", "estimate", "adj.p.value")]
anova.eff.results.df$Con <- Con

flextable(anova.eff.results.df,
          col_keys = c("Con", "estimate", "adj.p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(adj.p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", 
                    Con = "Contrast", estimate = "Diff. of Means",
                    adj.p.value = "Adjusted Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "adj.p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> table_g7

#Table G8
rep.anova = aov(represent_change ~ project_assignment, ds)
TukeyHSD(rep.anova, conf.level=.95) -> anova.rep.mod.results

anova.rep.mod.results.df <- map_df(list(anova.rep.mod.results), .id = "dv", tidy)
anova.rep.results.df <- anova.rep.mod.results.df[c("contrast", "estimate", "adj.p.value")]
anova.rep.results.df$Con <- Con

flextable(anova.rep.results.df,
          col_keys = c("Con", "estimate", "adj.p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(adj.p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", 
                    Con = "Contrast", estimate = "Diff. of Means",
                    adj.p.value = "Adjusted Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "adj.p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> table_g8

#Table G9
resp.anova = aov(responsible_change ~ project_assignment, ds)
TukeyHSD(resp.anova, conf.level=.95) -> anova.resp.mod.results

anova.resp.mod.results.df <- map_df(list(anova.resp.mod.results), .id = "dv", tidy)
anova.resp.results.df <- anova.resp.mod.results.df[c("contrast", "estimate", "adj.p.value")]
anova.resp.results.df$Con <- Con

flextable(anova.resp.results.df,
          col_keys = c("Con", "estimate", "adj.p.value", "signif")) %>% 
  colformat_double(digits = 3) %>% 
  mk_par(j = "signif", value = as_paragraph(pvalue_format(adj.p.value)) ) %>% 
  set_header_labels(dv = "Dependent Variable", 
                    Con = "Contrast", estimate = "Diff. of Means",
                    adj.p.value = "Adjusted Pr(>|t|)", signif = "" ) %>% 
  align(j = "signif", align = "left") %>% 
  padding(padding.right = 0, j = "adj.p.value", part  = "all") %>% 
  bold(j = "signif", bold = TRUE) %>% 
  padding(padding.left = 0, j = "signif", part  = "all") %>% 
  autofit() -> table_g9

